#include "taskqueue.hpp"

namespace moqi
{

//using elementType = int;
TaskQueue::TaskQueue(int size)
    :_que(),
    _queSize(size),
    _mutex(),
    _notFull(_mutex),
    _notEmpty(_mutex),
     _flag(true)
{


}
TaskQueue::~TaskQueue()
{

}

bool TaskQueue::empty()const
{
    return _que.size() == 0;
}

bool TaskQueue::full()const
{
    return _que.size() == _queSize;
}

void TaskQueue::wakeupAll()
{
    _flag = false;
    _notEmpty.notifyAll();
}
void TaskQueue::push(ElementType && element)
{
    _mutex.lock();

    while(full()){
        _notFull.wait();
    }
    _que.push(std::move(element));
    _notEmpty.notify();
    _mutex.unlock();
}

ElementType TaskQueue::TaskQueue::pop(){
    _mutex.lock();

    while(empty()&&_flag){
        _notEmpty.wait();
    }
    if(_flag)
    {
        ElementType temp = _que.front();
        _que.pop();
        _notFull.notify();
        _mutex.unlock();
        return temp;
    }
   else{
       _mutex.unlock();
        return nullptr;
   }
}

}
